//+------------------------------------------------------------------+ 
//|   TRO_HL5                                                        | 
//|                                                                  | 
//|   Copyright  2016, Avery T. Horton, Jr. aka TheRumpledOne       |
//|                                                                  |
//|   PO BOX 1673, BANDON, OR 97411                                  |
//|                                                                  |
//|   GIFTS AND DONATIONS ACCEPTED                                   | 
//|   All my indicators should be considered donationware. That is   |
//|   you are free to use them for your personal use, and are        |
//|   under no obligation to pay for them. However, if you do find   |
//|   this or any of my other indicators help you with your trading  |
//|   then any Gift or Donation as a show of appreciation is         |
//|   gratefully accepted.                                           |
//|                                                                  |
//|   Gifts or Donations also keep me motivated in producing more    |
//|   great free indicators. :-)                                     |
//|                                                                  |
//|   PayPal - THERUMPLEDONE@GMAIL.COM                               |  
//+------------------------------------------------------------------+ 
//| Use http://therumpledone.mbtrading.com/forex.aspx                |  
//| as your forex broker...tell them therumpledone sent you!         |  
//+------------------------------------------------------------------+ 
//| DO NOT DISTRIBUTE THIS CODE BY ANY MEANS - tell people to        |
//| go to www.kreslik.com for the latest indicator updates           |  
//+------------------------------------------------------------------+ 

#property  copyright "Copyright  2016, Avery T. Horton, Jr. aka TRO" 
#property  link      "http://www.therumpledone.com/" 

//+------------------------------------------------------------------+ 

#property indicator_chart_window

extern bool TURN_OFF    = false ;
extern int  Indicator_Tag=0;

extern bool Auto_Period = false ;

extern int myChartX   = 10 ;
extern int myChartY   = 100 ;  
  
extern int myCorner   = 1 ;
extern string myFont  = "Courier New" ;
extern int myFontSize = 12 ;

extern bool   Show_Gauge  = false ; 
extern bool   Show_Lines  = true ;
extern bool   Show_Lines_Right = false ;
extern bool   Show_Labels = true ;
extern bool   Show_Boxes  = true ;
extern bool   Show_Level  = false ;
extern bool   Show_Volume = false ;
extern bool   Show_Open   = false ;
extern bool   Show_High   = true ;
extern bool   Show_Low    = true ;
extern bool   Show_Close  = false ;
extern bool   Show_Mid    = false ;
extern bool   Show_TOP_BOTTOM   = false ;
extern bool   Show_Pivot  = false ;
extern bool   Show_PivotSR  = false ;
extern bool   Show_MidPivots  = false ;
extern bool   Show_PivotGridIron  = false ;
extern bool   Show_FixedPrice  = false ;
extern double FixedPrice         = 0.0 ;
extern bool   Show_VWAP = false ;
extern bool   OnlyShow_NotCrissCrossed = false ;
extern bool   Show_Side_Markers = false ;

extern bool   Show_HIGH_LOW_OFFSET  = false ;
extern double HIGH_LOW_OFFSET       = 20.0 ;

extern bool   Show_OPEN_OFFSET  = false ;
extern double OPEN_OFFSET       = 10.0 ;

extern bool   ShadeRight         = false ;
extern bool   ShadeDay           = false ;
extern bool   ShadePrevDay       = false ;
extern bool   ShadeBars          = false ;
extern int    BarsToShade        = 0 ;


extern bool   Show_Angle_Open   = false ;
extern bool   Show_Angle_High   = false ;
extern bool   Show_Angle_Low    = false ;
extern bool   Show_Angle_Close  = false ;
extern bool   Show_Angle_Mid    = false ;
extern bool   Show_Angle_Pivot  = false ; 
extern bool   DrawRay           = false ;    
extern int    Angle_Degrees     = 15 ; 
extern int    Angle_Bar_Shift   = 0 ;

extern bool   Show_Time_Lines   = false ;
extern int    myStartTimeHH = 14;
extern int    myStartTimeMM = 0;


extern bool   Show_Day_Lines   = false ;
input ENUM_DAY_OF_WEEK  DAYOFWEEK           = 1 ;  

extern bool   AllLinesBackground = true ;   
input ENUM_LINE_STYLE LineStyle    = STYLE_SOLID ;

extern int  LineWidth  =  0 ; 

input ENUM_TIMEFRAMES   myPeriod = 0 ; // myPeriod

extern int myBars   = 2 ;
extern int myShift  = 0 ;
            

extern bool   OnlyShow_Near = false ;
extern double myNearThreshold = 10.0 ;

extern double myThreshold = 10.0 ;

extern color colorHOD            = Red ;
extern color colorLOD            = Blue ;
extern color colorOpen           = Orange ;
extern color colorClose          = Chocolate ;
extern color colorMid            = Aqua ;
extern color colorTop            = clrBrown ;
extern color colorBottom         = clrBrown ;
extern color colorPivot          = Purple ;
extern color colorMidPivot       = clrViolet ;
extern color colorPivotGridIron  = clrYellow ;
extern color colorFixedPrice     = clrMediumAquamarine ;
extern color colorVWAP           = clrCadetBlue ;
extern color UpColor             = Lime;
extern color DownColor           = Red;
extern color EqColor             = DimGray;
extern color colorRat            = DimGray;
extern color colorBuyZone        = clrBlueViolet;

extern color colorShadeRight = clrWhiteSmoke ;
extern color colorShadeDay = clrGainsboro ;
extern color colorShadePrevDay = C'30,33,36' ;



//+------------------------------------------------------------------+


string aDay[7];

double Hi, Li, C;

double prev_high, prev_low, prev_close,prev_open, D, p, value ;
int h  ;
string lbl[99], lbl2[99];
int n, j, k, a ;

color pColor = Blue, VolColor = Magenta ;

string symbol, tChartPeriod, ShortName, TAG ;  
int    digits, period, win ; 
double point,  open, high, low, close, CLOSE, mid, range, pClose, pOpen, pHigh, pLow, pMid, pRange, cRange, PipDiff  ;  
double VolHH, VolLL, Vol, diff, TOP, BOTTOM ;
string VolMsg;

int FillAmt = 11 , x001, x002, x003, x004, x005, x006, x007 ;   

int plotbars = 0 ;

bool TestVol ;

double  HH, LL ;  
 
int    nHH, nLL ;

 
double YPP,FPP,PP,R1,R2,R3,S1,S2,S3,  TC,BC, R4,R5,R6,S4,S5,S6;
double M0,M1,M2,M3,M4,M5,N0,N1,N2,N3,N4,N5;   
double PP_20U,PP_20D,R1_20,R2_20,R3_20,R4_20,R5_20,R6_20,S1_20,S2_20,S3_20,S4_20,S5_20,S6_20 ;
double PP_80U,PP_80D,R1_80,R2_80,R3_80,R4_80,R5_80,R6_80,S1_80,S2_80,S3_80,S4_80,S5_80,S6_80 ;

string yymmddms, sMM, sHH, sHHMM, day, sTOP, sBOTTOM ;
datetime var1, DateTime  ;  
int barnum, LastHH, LastMM, UseHH, UseMM, yy,mm ;

double VWAP, xNear, xOffSet, xH, xL, xBZ, xSZ, xBuyZone;
 
datetime STARTTIME, ENDTIME ;    
double   PRICEMAX, PRICEMIN ;   
//+------------------------------------------------------------------+

int init()
{
   
   if(myPeriod == 0) { period = Period() ; } else { period = myPeriod ; }
       
 //  if(Auto_Period && period < Period() ) { period = Period() ; }
   
   tChartPeriod =  TimeFrameToString(period) ;
   symbol       =  Symbol() ;
   digits       =  Digits ;
   point        =  Point ;
   TAG          = "HL5"+Indicator_Tag ; 
   ShortName    = TAG+symbol+myPeriod ;
   win          = 0 ;

   if(digits == 5 || digits == 3) { digits = digits - 1 ; point = point * 10 ; }   
 
   
   if( myCorner == 0 || myCorner == 2 )
   { 
      x001 = 0 ;
      x002 = 0 ;
      x003 = 90 ; 
      x004 = 150;
      x005 = 100 ;
      x006 = 90 ;  
      x007 = 90 ;
      FillAmt = 20 ;     
   }
   else
   {
      x001 = 0 ;
      x002 = 130 ; //  
      x003 = 50 ;  //  
      x004 = 0 ; //  
      x005 = 10 ;
      x006 = 10 ;  
      x007 = 10 ;   
      FillAmt = 20 ; 
   }
           
   ObDeleteObjectsByPrefix(TAG); 
   deinit();   
   
   UseHH = myStartTimeHH ;
   UseMM = myStartTimeMM ;

   aDay[0] = "Sun" ;
   aDay[1] = "Mon" ;
   aDay[2] = "Tue" ;
   aDay[3] = "Wed" ;
   aDay[4] = "Thur" ;
   aDay[5] = "Fri" ;
   aDay[6] = "Sat" ;
   
   xNear =  myNearThreshold * point ;
   
   xOffSet = HIGH_LOW_OFFSET * point ;
   
   xBuyZone = OPEN_OFFSET * point ;
   
   return(0);
}
  
  
//+------------------------------------------------------------------+

void ObDeleteObjectsByPrefix(string Prefix)
{
   int ll = StringLen(Prefix);
   int ii = 0; 
   while(ii < ObjectsTotal())
     {
       string ObjName = ObjectName(ii);
       if(StringSubstr(ObjName, 0, ll) != Prefix) 
         { 
           ii++; 
           continue;
         }
       ObjectDelete(ObjName);
     }
}  
  
//+------------------------------------------------------------------+
int deinit()
{
    
   ObDeleteObjectsByPrefix(ShortName);          
   TRO() ;

   return(0);
}
  
//+------------------------------------------------------------------+
string sDiff( double d1, double d2)
{   
   
   double d0 ;
   
   d0 = ( d1 - d2 ) / point ;

   return( DoubleToStr(d0,0) );
}
  
//+------------------------------------------------------------------+
int start()
{
   
   deinit();
   if( TURN_OFF ) {  return(0) ; }
      
   a       = 0;
   n       = 10;
   j       = 0 ;
   
   if(Show_Gauge) 
   {
   lbl[j]  = ShortName+j;
   lbl2[j] = fFill(tChartPeriod + " HIGHs LOWs +/-", 22) ;
   DoShowHead(j, SteelBlue);
   }
   
   close    = iClose( symbol, 0, 0) ;
   
   for( k = myShift; k < myBars+myShift; k++ )
   {
   
      open     = iOpen( symbol, period, k ) ;
      high     = iHigh( symbol, period, k ) ;
      low      = iLow( symbol, period, k ) ;
      CLOSE    = iClose( symbol, period, k ) ;      
      pHigh    = iHigh( symbol, period, k+1 ) ;
      pLow     = iLow( symbol, period, k+1 ) ;
      pClose   = iClose( symbol, period, k+1 ) ;            
      
      mid      = (high + low) * 0.50 ;
 //     pivot    = (pHigh + pLow + pClose) * 0.333333 ;
 
      TOP      = MathMax(open,CLOSE) ;
      BOTTOM   = MathMin(open,CLOSE) ;
      
      if(TOP == open) { sTOP = "O-TOP " ; sBOTTOM = "C-BOTTOM " ; }
      else            { sTOP = "C-TOP " ; sBOTTOM = "O-BOTTOM " ; }
     
      DoPivot ( symbol, period, k+1 );
      
      YPP      = PP;
           
      DoPivot ( symbol, period, k );
      
      nHH     =  Highest(symbol,period,MODE_HIGH,k+1,myShift); 
      nLL     =  Lowest(symbol,period,MODE_LOW,k+1,myShift);     
    
      HH     = iHigh(symbol,period,nHH);      
      LL     = iLow(symbol,period,nLL);      
      
      FPP    = (high + low + CLOSE ) / 3.0 ;

   if(Show_FixedPrice && k == myBars+myShift-1)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && !( HH > FixedPrice && FixedPrice > LL )))
   {
   j       = j + 1 ;
   lbl[j]  = ShortName+j;
   lbl2[j] = "Fixed" ;
   PipDiff = ( FixedPrice - close ) / point ; 
   DoColor( close, high  ) ; 
   if(Show_Gauge) { DoShow(j, colorFixedPrice,FixedPrice ); }
   if(Show_Lines) { drawLine(FixedPrice,ShortName+"Fixed(" + k, colorFixedPrice, 2, "Fixed Price  " )   ; }
   }
   
   if(Show_High)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > high && high > LL )))
   {
   j       = j + 1 ;
   lbl[j]  = ShortName+j;
   lbl2[j] = "High[" + k + "]" ;
   PipDiff = ( high - close ) / point ; 
   DoColor( close, high  ) ; 
   if(Show_Gauge) { DoShow(j, colorHOD,high ); }
   if(Show_Lines) { drawLine(high,ShortName+"High(" + k, colorHOD, 2,tChartPeriod+"["+k+"] High  " )   ; }
   }

   int iBarD1=iBarShift(symbol,PERIOD_D1,iTime(symbol,period,k),false);
   datetime firstDailyTime=iTime(symbol,PERIOD_D1,iBarD1);
   datetime lastDailyTime=firstDailyTime+(PERIOD_D1/Period()-1)*60*Period(); 

   int firstDailyBar =iBarShift(symbol,Period(),firstDailyTime);
   int lastDailyBar  =iBarShift(symbol,Period(),lastDailyTime);
      
   int    nHO    =  Highest(symbol,Period(),MODE_HIGH,firstDailyBar - lastDailyBar + 1 ,lastDailyBar) ;  
   int    nLO    =  Lowest(symbol,Period(),MODE_LOW,firstDailyBar - lastDailyBar + 1 ,lastDailyBar)  ;  
   
   double     HO     = iHigh(symbol,Period(),nHO);  
   double     LO     = iLow(symbol,Period(),nLO);

 

   
   if(Show_Angle_High)   
   { 
      DrawAngle( high, colorHOD, Angle_Degrees,iTime(symbol,PERIOD_D1,Angle_Bar_Shift)  ) ; //  iTime(symbol,Period(),nHO)
      DrawAngle( high, colorHOD, 360-Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; //iTime(symbol,Period(),nHO)
   }
   
   if(Show_Low)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > low && low > LL )))
   {
   j       = j + 1 ;
   lbl[j]  = ShortName+j;
   lbl2[j] = "Low[" + k + "] " ;
   PipDiff = ( close - low ) / point ; 
   DoColor( close, low  ) ; 
   if(Show_Gauge) { DoShow(j, colorLOD, low); }
   if(Show_Lines) { drawLine(low,ShortName+"Low(" + k, colorLOD, 2,tChartPeriod+"["+k+"] Low  "  ) ; }
   }

   if(Show_Angle_Low) 
   { 
      DrawAngle( low, colorLOD, Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
      DrawAngle( low, colorLOD, 360-Angle_Degrees,iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
   }
    
   if(Show_Open)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > open && open > LL )))
   {
   j       = j + 1 ;
   lbl[j]  = ShortName+j;
   lbl2[j] = "Open[" + k + "] " ;
   PipDiff = ( close - open ) / point ; 
   DoColor( close, open  ) ; 
   if(Show_Gauge) { DoShow(j, colorOpen, open); }
   if(Show_Lines) { drawLine(open,ShortName+"open(" + k, colorOpen, 2,tChartPeriod+"["+k+"] Open "  ) ; }
    
   } // if
 

   if(Show_Angle_Open) 
   { 
      DrawAngle( open, colorOpen, Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
      DrawAngle( open, colorOpen, 360-Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
   } 
 
   
   if(Show_Close && k != 0 && close != iOpen( symbol, period, k+1 ))
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > close && open > close )))
      {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "Close[" + k + "] " ;
/*      
      PipDiff = ( close - pClose ) / point ; 
      DoColor( close, pClose  )  ;
      if(Show_Gauge) { DoShow(j, colorClose, pClose); }
      if(Show_Lines) { drawLine(pClose,ShortName+"close(" + k, colorClose, 2,tChartPeriod+"["+k+"] Close "  ) ; }
*/
      PipDiff = ( CLOSE - open ) / point ; 
      DoColor( CLOSE, open  )  ;
      if(Show_Gauge) { DoShow(j, colorClose, CLOSE); }
      if(Show_Lines) { drawLine(CLOSE,ShortName+"close(" + k, colorClose, 2,tChartPeriod+"["+k+"] Close "  ) ; }    
   } // if
   
   
   if(Show_Mid)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > mid && mid > LL )))
   {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "Mid[" + k + "] " ;
      PipDiff = ( close - mid ) / point ; 
      DoColor( close, mid  )  ;
      if(Show_Gauge) { DoShow(j, colorMid, mid); }
      if(Show_Lines) { drawLine(mid,ShortName+"mid(" + k, colorMid, 2,tChartPeriod+"["+k+"] Mid   "  ) ; }
    
   } // if

   if(Show_VWAP && k == 0 && period < PERIOD_W1)
   {
      VWAP = DoVWAP();
      
      if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > VWAP && VWAP > LL )))
      {
         j       = j + 1 ;
         lbl[j]  = ShortName+j;
         lbl2[j] = "VWAP" ;
         PipDiff = ( close - VWAP ) / point ; 
         DoColor( close, VWAP  )  ;
         if(Show_Gauge) { DoShow(j, colorVWAP, VWAP); }
         if(Show_Lines) { drawLine(VWAP,ShortName+"VWAP(" + k, colorVWAP, 2,"VWAP "  ) ; }
       
      } // if
   } // if

   if(Show_Angle_Mid) 
   { 
      DrawAngle( mid, colorMid, Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
      DrawAngle( mid, colorMid, 360-Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
   } 

    
   if(Show_TOP_BOTTOM && k != 0)
   if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > TOP && TOP > LL )))
   {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = sTOP+"[" + k + "] " ;
      PipDiff = ( close - TOP ) / point ; 
      DoColor( close, TOP  ) ; 
      if(Show_Gauge) { DoShow(j, colorTop, TOP); }
      if(Show_Lines) { drawLine(TOP,ShortName+"TOP(" + k, colorTop, 2,tChartPeriod+"["+k+"] " + sTOP  ) ; }
    
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = sBOTTOM + "[" + k + "] " ;
      PipDiff = ( close - BOTTOM ) / point ; 
      DoColor( close, BOTTOM  ) ; 
      if(Show_Gauge) { DoShow(j, colorBottom, BOTTOM); }
      if(Show_Lines) { drawLine(BOTTOM,ShortName+"BOTTOM(" + k, colorBottom, 2,tChartPeriod+"["+k+"] " + sBOTTOM  ) ; }

   } // if

    
   if(Show_Pivot && k == 0 && Period() <= period)
   if (!OnlyShow_NotCrissCrossed || 
   ( OnlyShow_NotCrissCrossed && k>0 && ( !( HH > PP && PP > LL )) && !( high > PP && PP > low ) ) )
   {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "PP[" + k + "] " ;
      PipDiff = ( close - PP ) / point ; 
      DoColor( close, PP  )  ;
      if(Show_Gauge) { DoShow(j, colorPivot, PP); }
      if(Show_Lines) 
      { 
         if( k == 0 && ( pHigh < YPP || pLow > YPP))
         {
            drawLine(YPP,ShortName+"YPP(" + k, colorPivot, 2,tChartPeriod+"["+1+"] PP MISSED  " ) ;          
         }  
           
         drawLine(FPP,ShortName+"FPP(" + k, colorPivot, 2,tChartPeriod+"["+k+"] FPP   "  ) ; 
         drawLine(PP,ShortName+"PP(" + k, colorPivot, 2,tChartPeriod+"["+k+"] PP   "  ) ; 
       if(Show_PivotSR)
       {
         drawLine(R1,ShortName+"R1(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R1   "  ) ; 
         drawLine(R2,ShortName+"R2(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R2   "  ) ; 
         drawLine(R3,ShortName+"R3(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R3   "  ) ; 
         drawLine(S1,ShortName+"S1(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S1   "  ) ; 
         drawLine(S2,ShortName+"S2(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S2   "  ) ; 
         drawLine(S3,ShortName+"S3(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S3   "  ) ;       
         drawLine(R4,ShortName+"R4(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R4   "  ) ; 
         drawLine(R5,ShortName+"R5(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R5   "  ) ; 
         drawLine(R6,ShortName+"R6(" + k, colorPivot, 2,tChartPeriod+"["+k+"] R6   "  ) ; 
         drawLine(S4,ShortName+"S4(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S4   "  ) ; 
         drawLine(S5,ShortName+"S5(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S5   "  ) ; 
         drawLine(S6,ShortName+"S6(" + k, colorPivot, 2,tChartPeriod+"["+k+"] S6   "  ) ; 
         if(!Show_Mid || (Show_Mid && TC != mid) ) {drawLine(TC,ShortName+"TC(" + k, colorPivot, 2,tChartPeriod+"["+k+"] TC   "  ) ; }
         if(!Show_Mid || (Show_Mid && BC != mid) ) {drawLine(BC,ShortName+"BC(" + k, colorPivot, 2,tChartPeriod+"["+k+"] BC   "  ) ; }
       }        
         if(Show_MidPivots)
         {
            drawLine(M1,ShortName+"M1(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS2   "  ) ; 
            drawLine(M2,ShortName+"M2(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS1   "  ) ; 
            drawLine(M3,ShortName+"M3(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR1   "  ) ; 		
            drawLine(M4,ShortName+"M4(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR2   "  ) ; 
            drawLine(M5,ShortName+"M5(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR3   "  ) ; 
            drawLine(M0,ShortName+"M0(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS3   "  ) ;  
            drawLine(N1,ShortName+"N1(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS5   "  ) ; 
            drawLine(N2,ShortName+"N2(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS6   "  ) ; 
            drawLine(N3,ShortName+"N3(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR4   "  ) ; 		
            drawLine(N4,ShortName+"N4(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR5   "  ) ; 
            drawLine(N5,ShortName+"N5(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mR6   "  ) ; 
            drawLine(N0,ShortName+"N0(" + k, colorMidPivot, 2,tChartPeriod+"["+k+"] mS4   "  ) ;                        
         }
         if(Show_PivotGridIron)
         {         
            drawLine(R5_80,ShortName+"R5_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R6 20-   "  ) ;         
            drawLine(R5_20,ShortName+"R5_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R5 20+   "  ) ;    
            drawLine(R4_80,ShortName+"R4_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R5 20-   "  ) ;         
            drawLine(R4_20,ShortName+"R4_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R4 20+   "  ) ;    

            drawLine(R3_80,ShortName+"R3_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R4 20-   "  ) ;         
            drawLine(R3_20,ShortName+"R3_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R3 20+   "  ) ;    
            drawLine(R2_80,ShortName+"R2_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R3 20-   "  ) ;         
            drawLine(R2_20,ShortName+"R2_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R2 20+   "  ) ;   
            drawLine(R1_80,ShortName+"R1_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R2 20-   "  ) ;         
            drawLine(R1_20,ShortName+"R1_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R1 20+   "  ) ;   


            drawLine(PP_20U,ShortName+"PP_20U(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] PP 20+   "  ) ;   
            drawLine(PP_20D,ShortName+"PP_20D(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] PP 20-   "  ) ;         
            drawLine(PP_80U,ShortName+"PP_80U(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] R1 20-   "  ) ;   
            drawLine(PP_80D,ShortName+"PP_80D(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S1 20+   "  ) ;         



            drawLine(S1_20,ShortName+"S1_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S1 20+   "  ) ;                                            
//            drawLine(S1_80,ShortName+"S1_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S2 20+   "  ) ;         
            drawLine(S2_20,ShortName+"S2_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S2 20+   "  ) ;      
            drawLine(S2_80,ShortName+"S2_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S1 20-   "  ) ;         
            drawLine(S3_20,ShortName+"S3_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S3 20+   "  ) ;                                            
            drawLine(S3_80,ShortName+"S3_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S2 20-   "  ) ;         
            drawLine(S4_20,ShortName+"S4_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S4 20+   "  ) ;                                            
            drawLine(S4_80,ShortName+"S4_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S3 20-   "  ) ;         
            drawLine(S5_20,ShortName+"S5_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S5 20+   "  ) ;                                            
            drawLine(S5_80,ShortName+"S5_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S4 20-   "  ) ;         
            drawLine(S6_20,ShortName+"S6_20(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S6 20+   "  ) ;                                            
            drawLine(S6_80,ShortName+"S6_80(" + k, colorPivotGridIron, 2,tChartPeriod+"["+k+"] S5 20-   "  ) ;         
        
 
         }     
      }
      
                     
   } // if    

   if(Show_Angle_Pivot) 
   { 
      DrawAngle( PP, colorPivot, Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
      DrawAngle( PP, colorPivot, 360-Angle_Degrees, iTime(symbol,PERIOD_D1,Angle_Bar_Shift) ) ; 
   } 

    
   if(Show_Time_Lines)
   {
      
      if( UseHH < 10) { sHH = "0"+UseHH ; } else { sHH = UseHH ; }    
      if( UseMM < 10) { sMM = "0"+UseMM ; } else { sMM = UseMM ; } 
      sHHMM = sHH + ":" + sMM ; 
   
      yymmddms = StringSubstr(TimeToStr(TimeCurrent() ),0,10) +" "+ sHHMM ;
      
      var1     = StrToTime(yymmddms);    
         
      if(var1 < TimeCurrent() && TimeHour( var1 ) < TimeHour( TimeCurrent() ) )
      {    
         barnum    = iBarShift(symbol,myPeriod,var1,TRUE);
         
         open      = iOpen(symbol,myPeriod,barnum) ;
         high      = iHigh(symbol,myPeriod,barnum) ; 
         low       = iLow(symbol,myPeriod,barnum) ;
         close     = iClose(symbol,myPeriod,barnum) ;
         
         mid       = ( high + low ) * 0.50 ;

         if(Show_High) drawLine(high,ShortName+"HighHHMM" + sHHMM, colorHOD, 2,sHHMM + " " + tChartPeriod + " High  " )   ; 
         if(Show_Mid) drawLine(mid,ShortName+"MidHHMM" + sHHMM, colorMid, 2,sHHMM + " " + tChartPeriod +  " Mid  "  ) ;       
         if(Show_Low) drawLine(low,ShortName+"LowHHMM" + sHHMM, colorLOD, 2,sHHMM + " " + tChartPeriod +  " Low  "  ) ;       
         if(Show_Open) drawLine(open,ShortName+"openHHMM" + sHHMM, colorOpen, 2,sHHMM + " " + tChartPeriod +  " Open "  ) ; 
         if(Show_Close) drawLine(close,ShortName+"CloseHHMM" + sHHMM, colorClose, 2,sHHMM + " " + tChartPeriod +  " Close "  ) ; 
  
      } 
   }    

   VolMsg   = " ";
   
   if(Show_Volume)
   {
      TestVol = MathAbs((close - high) / point ) < myThreshold || MathAbs((close - low) / point ) < myThreshold ;
    
      if(k > 0 && TestVol )
      {
         Vol   = iVolume(symbol,period,0);     
         VolHH = iVolume(symbol,period,k);   
      
         if( Vol >=  VolHH ) { VolMsg = "* Higher Volume("+k+")"; VolColor = UpColor ;}
         else { VolMsg = "* Lower Volume("+k+")"; VolColor = DownColor ;}
      } // if
   } // if
    
   if(VolMsg != " ")
   {
   j       = j + 1 ;
   lbl[j]  = ShortName+j;
   lbl2[j] = fFill(VolMsg, 20) ;
   DoShowHead(j, VolColor);
   }


   if(Show_HIGH_LOW_OFFSET)
   {
      xH = high - xOffSet ;
      xL = low + xOffSet ;
     
      if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > xH && xH > LL )))
      {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "R-RAT[" + k + "] " ;
      PipDiff = ( close - xH ) / point ; 
      DoColor( close, xH  ) ; 
      if(Show_Gauge) { DoShow(j, colorRat, xH); }
      if(Show_Lines) { drawLine(xH,ShortName+"xH(" + k, colorRat, 2,tChartPeriod+"["+k+"] R-RAT  "  ) ; }
      }

      if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > xL && xL > LL )))
      {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "G-RAT[" + k + "] " ;
      PipDiff = ( close - xL ) / point ; 
      DoColor( close, xL  ) ; 
      if(Show_Gauge) { DoShow(j, colorRat, xL); }
      if(Show_Lines) { drawLine(xL,ShortName+"xL(" + k, colorRat, 2,tChartPeriod+"["+k+"] G-RAT  "  ) ; }
      }

   }
   
   if(Show_OPEN_OFFSET)
   {
      xBZ = open + xBuyZone ;
      xSZ = open - xBuyZone ;
     
      if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > xBZ && xBZ > LL )))
      {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "BZ[" + k + "] " ;
      PipDiff = ( close - xBZ ) / point ; 
      DoColor( close, xBZ  ) ; 
      if(Show_Gauge) { DoShow(j, colorBuyZone, xBZ); }
      if(Show_Lines) { drawLine(xBZ,ShortName+"xBZ(" + k, colorBuyZone, 2,tChartPeriod+"["+k+"] BZ  "  ) ; }
      }

      if (!OnlyShow_NotCrissCrossed || ( OnlyShow_NotCrissCrossed && k>0 && !( HH > xSZ && xSZ > LL )))
      {
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "G-RAT[" + k + "] " ;
      PipDiff = ( close - xSZ ) / point ; 
      DoColor( close, xSZ  ) ; 
      if(Show_Gauge) { DoShow(j, colorBuyZone, xSZ); }
      if(Show_Lines) { drawLine(xSZ,ShortName+"xSZ(" + k, colorBuyZone, 2,tChartPeriod+"["+k+"] xSZ  "  ) ; }
      }

   }

    
   } // for
   
 
   if(Show_Day_Lines)
   {
 
   for (int dd=0;dd<8;dd++)
      {
        DateTime = iTime(symbol,PERIOD_D1,dd) ;
        
        if( TimeDayOfWeek(DateTime) == DAYOFWEEK )
        {
           barnum = iBarShift(symbol,PERIOD_D1,DateTime) ;
           open   = iOpen(symbol,PERIOD_D1,barnum) ;
           high   = iHigh(symbol,PERIOD_D1,barnum) ;
           low    = iLow(symbol,PERIOD_D1,barnum) ;
           close  = iClose(symbol,PERIOD_D1,barnum) ;
           
           
           day  = aDay[TimeDayOfWeek(DateTime)] ;
           break ;
        }
      }   
    
   drawLine(open,ShortName+"OpenDAY"  , colorOpen, 2, day + " Open " )   ; 
   drawLine(low,ShortName+"LowDAY"  , colorLOD, 2, day  +  " Low "  ) ;       
   drawLine(high,ShortName+"HighDAY"  , colorHOD, 2, day + " High " )   ; 
   drawLine(close,ShortName+"CloseDAY"  , colorClose, 2, day  +  " Close "  ) ;       

   }   
    
   DoShading();
   
   WindowRedraw() ;   
   
   return(0);
}
//+-------------------------------------------------------------------------------------------+      
void DoShading()
{   
   if(ShadeRight)
   {
      colorbox ("RTbox", Time[0], Time[0]+PERIOD_MN1*10000, WindowPriceMax(), WindowPriceMin(), colorShadeRight) ;
   }
   
   if(ShadeDay)
   {   
      PRICEMAX    = iHigh(symbol,PERIOD_D1,0) ; 
      PRICEMIN    = iLow(symbol,PERIOD_D1,0) ; 
      STARTTIME   = iTime(symbol,PERIOD_D1,0) ;
      colorbox ("Daybox", STARTTIME, Time[0] , PRICEMAX, PRICEMIN, colorShadeDay) ;
   }     
      
   if(ShadeBars)
   {   
      PRICEMAX    = 99999.0 ; 
      PRICEMIN    = 0.0 ; 
      STARTTIME   = iTime(symbol,period,BarsToShade-1) ;
      colorbox ("Daysbox", STARTTIME, Time[0] ,  WindowPriceMax(), WindowPriceMin(), colorShadeDay) ;
   }     
          
       
   return;
   
}
//+-------------------------------------------------------------------------------------------+      
void colorbox (string name, double starttime, double endtime, double CBhigh, double CBlow, color col2)
   {
   string box = TAG + name;
   if (ObjectFind(box) != 0){
      ObjectCreate(box,OBJ_RECTANGLE,0,0,0);
      ObjectSet(box,OBJPROP_TIME1,starttime);
      ObjectSet(box,OBJPROP_TIME2,endtime);
      ObjectSet(box,OBJPROP_PRICE1,CBhigh);
      ObjectSet(box,OBJPROP_PRICE2,CBlow);
      ObjectSet(box,OBJPROP_COLOR,col2);}
   else{      
      ObjectMove(box, 0, starttime,CBhigh);
      ObjectMove(box, 1, endtime, CBlow);}                             
   }
//+------------------------------------------------------------------+
  
void DoShowHead( int u, color dsColor )
{
   string rlabelH = lbl[u] + "Head" ;
   ObjectCreate(rlabelH,23,0,Time[0],PRICE_CLOSE);
   ObjectSet(rlabelH, OBJPROP_CORNER, myCorner );
   ObjectSet(rlabelH,OBJPROP_XDISTANCE, x001+myChartX);
   ObjectSet(rlabelH,OBJPROP_YDISTANCE,n+myChartY);
   ObjectSetText(rlabelH,lbl2[u],myFontSize,myFont,dsColor);
   
   n = n+20;


}
//+------------------------------------------------------------------+
  
void DoShow( int u, color dsColor, double dsPrice )
{
   ObjectCreate(lbl[u],23,0,Time[0],PRICE_CLOSE);
   ObjectSet(lbl[u], OBJPROP_CORNER, myCorner );
   ObjectSet(lbl[u],OBJPROP_XDISTANCE,x002+myChartX);
   ObjectSet(lbl[u],OBJPROP_YDISTANCE,n+myChartY);
   ObjectSetText(lbl[u],fFill(lbl2[u],9),myFontSize,myFont,dsColor);
   
   string Obj001 = lbl[u] + "price" ;
   string sVal = fFill(DoubleToStr(dsPrice,digits), 7)    ;
   ObjectCreate(Obj001,23,0,Time[0],PRICE_CLOSE);
   ObjectSet(Obj001, OBJPROP_CORNER, myCorner );
   ObjectSet(Obj001,OBJPROP_XDISTANCE, x003+myChartX);
   ObjectSet(Obj001,OBJPROP_YDISTANCE,n+myChartY);
   ObjectSetText(Obj001,sVal,myFontSize,myFont,dsColor);
   
   
   string Obj002 = lbl[u] + "avg" ;
   string sDif =  rtadjust( DoubleToStr(PipDiff,0) ) ;
   ObjectCreate(Obj002,23,0,Time[0],PRICE_CLOSE);
   ObjectSet(Obj002, OBJPROP_CORNER, myCorner );
   ObjectSet(Obj002,OBJPROP_XDISTANCE, x004+myChartX);
   ObjectSet(Obj002,OBJPROP_YDISTANCE,n+myChartY);
   ObjectSetText(Obj002,sDif,myFontSize,myFont,pColor);
   
   n = n+20;
   
}

//+------------------------------------------------------------------+
  
void DoColor( double c1, double c2 )
{

   if(c1 > c2 ) { pColor = UpColor ; } else {
   if(c1 < c2 ) { pColor = DownColor ; } else { pColor = EqColor ; } } 

}  
//+------------------------------------------------------------------+  
 
void DrawAngle( double aPrice, color aColor, int aDegrees, datetime aTime )
{  
   
   string Obj0002;
   
   a = a + 1;
   
   Obj0002 = ShortName+aDegrees+a ;
  
   
    
   ObjectCreate(Obj0002,OBJ_TRENDBYANGLE,0,0,0,0);
   ObjectSet(Obj0002,OBJPROP_COLOR,aColor); 
   ObjectSet(Obj0002,OBJPROP_BACK,AllLinesBackground); 
   ObjectSet(Obj0002,OBJPROP_WIDTH,1); 
   ObjectSet(Obj0002,OBJPROP_STYLE,STYLE_SOLID);
   ObjectSet(Obj0002,OBJPROP_TIME1,aTime);   
   ObjectSet(Obj0002,OBJPROP_PRICE1,aPrice); 
   ObjectSet(Obj0002,OBJPROP_ANGLE,aDegrees); 
   ObjectSet(Obj0002,OBJPROP_TIME2,Time[0]);    
   ObjectSet(Obj0002,OBJPROP_PRICE2,0); 
   ObjectSet(Obj0002,OBJPROP_RAY,DrawRay);   
}       

//+------------------------------------------------------------------+
void drawLine(double lvl,string name, color Col,int type,string text)
{
   datetime StartTime, EndTime;     
   string pipdist ;
   bool   RayRight ;

   if( OnlyShow_Near && (MathAbs(close - lvl) > xNear ) ) { return ; }
 
   if( StringFind(name,"HHMM",0) > -1 ) { StartTime = var1 ; } 
   else
   if( StringFind(name,"Fixed",0) > -1 ) { StartTime = iTime(symbol,period,Bars) ; } 
   else { StartTime = iTime(symbol,period,k) ; }
 
// ADJUSTING FOR BEGINNING OF WEEK/MONTH FOR PLOTTING
   if(period > PERIOD_D1)
   {        
      int  day1bar   = iBarShift(symbol,PERIOD_D1,StartTime,true);      
   
      if(day1bar == -1) 
      {        
         StartTime = StartTime+86400 ;
         day1bar   = iBarShift(symbol,PERIOD_D1,StartTime,true);  
         
         if(day1bar == -1) 
         {        
            StartTime = StartTime+86400 ;    
         }   
      } 
   } 
      
   
   EndTime  = Time[0] ;
   RayRight = false ;
   
   if( Show_Lines_Right )
   { 
      StartTime   = Time[1] ;
      EndTime     = Time[0] ;
      RayRight    = TRUE ;   
   }  
 
   ObjectDelete(name);
   ObjectCreate(name, OBJ_TREND, 0, StartTime, lvl,EndTime,lvl);
   ObjectSet(name, OBJPROP_STYLE, LineStyle);    
   ObjectSet(name, OBJPROP_COLOR, Col);        
   ObjectSet(name,OBJPROP_WIDTH,LineWidth);
   ObjectSet(name,OBJPROP_BACK,AllLinesBackground); 
   ObjectSet(name,OBJPROP_RAY_RIGHT,RayRight); 
   
     
            
   if(Show_Level)
   {
            ObjectDelete(name+"X");
            ObjectCreate(name+"X", OBJ_HLINE, 0, 0, lvl );
            ObjectSet(name+"X", OBJPROP_STYLE, 5);    
            ObjectSet(name+"X", OBJPROP_COLOR, Col);        
   }
   
     //Determine the current chart scale (chart scale number should be 0-5) 
     

   if(Show_Labels)
   {         
     string Obj0002 = name+"linelbl" ;
     double lclose ;
     
     int Chart_Scale  ,Bar_Width  ;      
     Chart_Scale = ChartScaleGet();
     
     //Set bar widths             
           if(Chart_Scale == 0) {Bar_Width = 120;}
     else {if(Chart_Scale == 1) {Bar_Width = 60;}      
     else {if(Chart_Scale == 2) {Bar_Width = 30;}
     else {if(Chart_Scale == 3) {Bar_Width = 15;}
     else {if(Chart_Scale == 4) {Bar_Width = 8;}
     else {Bar_Width = 4;} }}}}
 
     datetime LabelTime; 
     int LabelPeriod;
     
     if(Period() < PERIOD_MN1) { LabelPeriod = Period(); } else { LabelPeriod = PERIOD_MN1 + 10000 ; } 
     
     LabelTime = Time[0]+LabelPeriod*60*Bar_Width ; // Time[WindowFirstVisibleBar()/2]; // Time[0]+Period()*60*ShiftLabel ;   
 

   
   
      lclose  = iClose(symbol,PERIOD_D1,0) ;
      pipdist = sDiff( lclose , lvl  )   ;  // ( lvl , close ) close , lvl 
            
      ObjectDelete(Obj0002);
      
      if(ObjectFind(Obj0002) != 0)
      {
         ObjectCreate(Obj0002, OBJ_TEXT, 0, LabelTime, lvl);
         ObjectSetText(Obj0002,fFill(text+pipdist,22) , 8, "Terminal", Col);
      }
      else
      {
         ObjectMove(Obj0002, 0, LabelTime, lvl);
      } // if 
                 
   } // if     
   
   if(Show_Boxes)
   {  
     string dName = name+"boxes" ;
     if (ObjectFind(dName) != 0)
      {
          ObjectCreate(dName,OBJ_ARROW,0,Time[0],lvl);
          ObjectSet(dName,OBJPROP_ARROWCODE,SYMBOL_RIGHTPRICE);
          ObjectSet(dName,OBJPROP_COLOR,Col);  
      } 
      else
      {
         ObjectMove(dName,0,Time[0],lvl);
      } 
   } // if   
   
   if(Show_Side_Markers)
   {      
      ObjectDelete(name+"sm") ;
      ObjectCreate(name+"sm",OBJ_HLINE,0,0,lvl);
      ObjectSet(name+"sm",OBJPROP_COLOR,Col);
      ObjectSet(name+"sm",OBJPROP_HIDDEN,FALSE);
      ObjectSet(name+"sm",OBJPROP_STYLE,5);         
      ObjectSet(name+"sm",OBJPROP_BACK,false);  
   }        
           
}
//+-------------------------------------------------------------------------------------------+
//| Subroutine:  Get the chart scale number                                                   |
//+-------------------------------------------------------------------------------------------+
int ChartScaleGet()
{
  long result = -1;
  ChartGetInteger(0,CHART_SCALE,0,result);
  return((int)result);
}
//+------------------------------------------------------------------+
  
string TimeFrameToString(int tf)
{
   string tfs;
   switch(tf) {
      case PERIOD_M1:  tfs="M1"  ; break;
      case PERIOD_M5:  tfs="M5"  ; break;
      case PERIOD_M15: tfs="M15" ; break;
      case PERIOD_M30: tfs="M30" ; break;
      case PERIOD_H1:  tfs="H1"  ; break;
      case PERIOD_H4:  tfs="H4"  ; break;
      case PERIOD_D1:  tfs="D1"  ; break;
      case PERIOD_W1:  tfs="W1"  ; break;
      case PERIOD_MN1: tfs="MN";
   }
   return(tfs);
}

//+------------------------------------------------------------------+

string fFill(string filled, int f ) 
{
   string FILLED ;
   
   FILLED = StringSubstr(filled + "                                         ",0,f) ;
   
   return(FILLED);
}

//+------------------------------------------------------------------+
string rtadjust( string rString )
{
 
 int sl = StringLen(rString) ;
 
 while(true)
 {
   if( sl == 6) { break ; } 
   if( sl == 5) { rString = " " + rString ; break ; } 
   if( sl == 4) { rString = "  " + rString ; break ; }    
   if( sl == 3) { rString = "   " + rString ; break ; } 
   if( sl == 2) { rString = "    " + rString ; break ; } 
   if( sl == 1) { rString = "     " + rString ; break ; } 
   break ;
 }

   return(rString) ;
}       
 
//+------------------------------------------------------------------+  
void DoPivot( string bPair, int bPeriod, int bShift )
{

   if( bPeriod < PERIOD_D1 ) { bPeriod = PERIOD_D1 ; } 

   double b_open, b_high, b_low, b_close, XX ;
 
   b_open      = iOpen(bPair,bPeriod,bShift+1) ;  
   b_high      = iHigh(bPair,bPeriod,bShift+1) ;
   b_low       = iLow(bPair,bPeriod,bShift+1) ;   
   b_close     = iClose(bPair,bPeriod,bShift+1) ;
         
   PP = (b_high+b_low+b_close)/3.0;
   R1 = 2*PP-b_low;
   R2 = PP+(b_high - b_low);
   R3 = (2*PP)+(b_high-(2*b_low));
      
   S1 = 2*PP-b_high;
   S2 = PP-(b_high - b_low);
   S3 = (2*PP)-((2*b_high)-b_low);

   BC = ( b_high + b_low ) * 0.50 ;
   TC = (PP - BC) + PP ; 

   if( BC > TC ) { XX = BC ; BC = TC ;  TC = XX ; }

	R4 = (3*PP)+(b_high-(3*b_low));
	S4 = (3*PP)-((3* b_high)-b_low);
	R5 = (4*PP)+(b_high-(4*b_low));
	S5 = (4*PP)-((4* b_high)-b_low);
	R6 = (5*PP)+(b_high-(5*b_low));
	S6 = (5*PP)-((5* b_high)-b_low);

   M5 = (R2+R3)/2;
   M4 = (R1+R2)/2;
   M3 = (PP+R1)/2;
   M2 = (PP+S1)/2;
   M1 = (S1+S2)/2;	
   M0 = (S2+S3)/2;
   N0 = (S4+S3)/2;
   N1 = (S4+S5)/2;
   N2 = (S6+S5)/2;
   N3 = (R4+R3)/2;	
   N4 = (R4+R5)/2;
   N5 = (R6+R5)/2;

   PPG( PP, R1, PP_20U, PP_80U ) ;
   PPG( S1, PP, PP_80D, PP_20D ) ;

   PPG( S1, PP, S1_20, S1_80 ) ; 
   PPG( S2, S1, S2_20, S2_80 ) ;   
   PPG( S3, S2, S3_20, S3_80 ) ;   
   PPG( S4, S3, S4_20, S4_80 ) ;   
   PPG( S5, S4, S5_20, S5_80 ) ;   
   PPG( S6, S5, S6_20, S6_80 ) ;    
       
   PPG( R1, R2, R1_20, R1_80 ) ; 
   PPG( R2, R3, R2_20, R2_80 ) ;   
   PPG( R3, R4, R3_20, R3_80 ) ;
   PPG( R4, R5, R4_20, R4_80 ) ;   
   PPG( R5, R6, R5_20, R5_80 ) ; 
   
               
   return;      
} 
//+------------------------------------------------------------------+  
 
void PPG( double p1, double p2, double &g20, double &g80 )
{    
   g20 = p1 + (( p2 - p1 ) * 0.20) ;  
   g80 = p1 + (( p2 - p1 ) * 0.80) ; 
   return;      
} 
//+------------------------------------------------------------------+  
double DoVWAP()
{       
   int    iStartBar;
   double Sum    = 0.0;
   double Weight = 0.0;
   double vwma   = 0.0;      

   iStartBar = iBarShift(symbol, PERIOD_M1,iTime(symbol, PERIOD_D1,0),false) ;

   for(int b = 0; b < iStartBar; b++)
   { 
      Weight+= iVolume(symbol,PERIOD_M1,b);
      
      Sum += getAppliedPrice(PRICE_TYPICAL, b,symbol, PERIOD_M1)*iVolume(symbol,PERIOD_M1,b);
   }
   
   if(Weight>0.0) { vwma = Sum/Weight; } else { vwma = 0.0; } 
  
   return(vwma);      
} 

//+------------------------------------------------------------------+
double getAppliedPrice(int nAppliedPrice, int nIndex, string bPair ,int bPeriod)
{
   double dPrice;
   //----
   switch(nAppliedPrice)
   {
      case 0:  dPrice=iClose(bPair,bPeriod, nIndex) ;  break;
      case 1:  dPrice=iOpen(bPair,bPeriod, nIndex) ;  break;
      case 2:  dPrice=iHigh(bPair,bPeriod, nIndex) ;  break;
      case 3:  dPrice=iLow(bPair,bPeriod, nIndex) ;  break;
      case 4:  dPrice=(iHigh(bPair,bPeriod, nIndex)+iLow(bPair,bPeriod, nIndex))/2.0;                 break;
      case 5:  dPrice=(iHigh(bPair,bPeriod, nIndex)+iLow(bPair,bPeriod, nIndex)+iClose(bPair,bPeriod, nIndex))/3.0;   break;
      case 6:  dPrice=(iHigh(bPair,bPeriod, nIndex)+iLow(bPair,bPeriod, nIndex)+2*iClose(bPair,bPeriod, nIndex))/4.0; break;
      default: dPrice=0.0;
   }
   //----
   return(dPrice);
}

//+------------------------------------------------------------------+  

enum ENUM_CRITERIA_TYPE
  {
   D1,       // PERIOD_D1
   W1,       // PERIOD_W1
   MN1,      // PEIROD_MN1
   Y1,       // PERIOD_Y1
  };
  

 datetime GetCriteriaTime(datetime t, ENUM_CRITERIA_TYPE TYPE)
     {
      datetime time;
      switch(TYPE)
        {
         case Y1:
            time = t - (t % 86400) - ((TimeDayOfYear(t)-1) * 86400);
            while(TimeYear(t)!=TimeYear(iTime(Symbol(),0,iBarShift(Symbol(),0,time)))) time=time+Period();
            return time;
            break;
         case MN1:
            time=t -(t%86400) -((TimeDay(t)-1)*86400);
            while(iBarShift(Symbol(),Period(),time,true)==-1) time=time+Period();
            return time;
            break;
         case W1:
            return t - (t % 86400) - (TimeDayOfWeek(t) * 86400);
            break;
         case D1:
            return t - (t % 86400);
            break;
         default:
            return t - (t % (PeriodSeconds(Period())));
        }
     }

 
//+------------------------------------------------------------------+
void TRO()
{   
   
   string tObjName03    = "TROTAG"  ;  
   ObjectCreate(tObjName03, OBJ_LABEL, 0, 0, 0);//HiLow LABEL
   ObjectSetText(tObjName03, CharToStr(78) , 12 ,  "Wingdings",  DimGray );
   ObjectSet(tObjName03, OBJPROP_CORNER, 3);
   ObjectSet(tObjName03, OBJPROP_XDISTANCE, 5 );
   ObjectSet(tObjName03, OBJPROP_YDISTANCE, 5 );  
}


//+------------------------------------------------------------------+

/*
 
     Comment("LabelTime " , LabelTime ,   "\n" , 
         "Bar_Width " , Bar_Width ,  "\n" ,
         "Period() " , Period() ,  "\n" ,
         "Time[0]) " , Time[0] ,  "\n" ,
         
         
   "") ;  
      j       = j + 1 ;
      lbl[j]  = ShortName+j;
      lbl2[j] = "Pivot[" + k + "] " ;
      PipDiff = ( close - pivot ) / point ; 
      DoColor( close, pivot  )  ;
      if(Show_Gauge) { DoShow(j, colorPivot, pivot); }
      if(Show_Lines) { drawLine(pivot,ShortName+"pivot(" + k, colorPivot, 2,tChartPeriod+"["+k+"] Pivot   "  ) ; }
    


     Comment("k " , k , "\n" , "iTime(symbol,period,k) " + TimeToStr(iTime(symbol,period,k)) );
     
if( k == 1)
{
   Comment(
         "k " , k , "\n" ,   
         "high " , DoubleToStr(high,Digits) , "\n" ,
         "HH " , DoubleToStr(HH,Digits) , "\n" ,
         "nHH " , nHH , "\n" ,   
         "LL " , DoubleToStr(LL,Digits) , "\n" ,
         "low " , DoubleToStr(low,Digits) , "\n" ,
         "nLL " , nLL , "\n" , 
  
         "nHO " , nHO , "\n" ,   
         "nLO " , nLO , "\n" ,  
         
         "HO " , DoubleToStr(HO,Digits) , "\n" ,                   
         "LO " , DoubleToStr(LO,Digits) , "\n" ,

         "firstDailyTime " , TimeToStr(firstDailyTime ) , "\n" ,
         "lastDailyTime " , TimeToStr(lastDailyTime ) , "\n" ,
         "firstDailyBar " , firstDailyBar , "\n" ,   
         "lastDailyBar " , lastDailyBar , "\n" , 
         
   "") ;  
}   
   
if( k == 1)
{
   Comment(
   "k " , k , "\n" ,   
   "high " , DoubleToStr(high,Digits) , "\n" ,
   "HH " , DoubleToStr(HH,Digits) , "\n" ,
   "pivot " , DoubleToStr(pivot,Digits) , "\n" ,
   "LL " , DoubleToStr(LL,Digits) , "\n" ,
   "low " , DoubleToStr(low,Digits) , "\n" ,
   
   "") ;  
}


  
//+------------------------------------------------------------------+
void drawLabel(string name,double lvl,color Color)
{
    if(ObjectFind(name) != 0)
    {
        ObjectCreate(name, OBJ_TEXT, 0, Time[10], lvl);
        ObjectSetText(name, name, 14, "Arial", EMPTY);
        ObjectSet(name, OBJPROP_COLOR, Color);
    }
    else
    {
        ObjectMove(name, 0, Time[10], lvl);
    }
}


Comment(
 
"MathAbs((close - high) / point " , DoubleToStr(MathAbs((close - high) / point),Digits)  , "\n" , 
"MathAbs((close - low) / point " , DoubleToStr(MathAbs((close - low) / point),Digits)  , "\n" ,  

"myThreshold " , DoubleToStr(myThreshold,Digits) , "\n" ,
"TestVol " , TestVol , "\n" , 
"") ;  
 
 
         if(ObjectFind(name) != 0)
         {
            ObjectCreate(name, OBJ_TREND, 0, StartTime, lvl,Time[0],lvl);
            
  
            ObjectSet(name, OBJPROP_STYLE, LineStyle);
            
            ObjectSet(name, OBJPROP_COLOR, Col);
            ObjectSet(name,OBJPROP_WIDTH,LineWidth);
            ObjectSet(name,OBJPROP_RAY,false);
            ObjectSet(name,OBJPROP_BACK,AllLinesBackground); 
            
         }
         else
         {
            ObjectDelete(name);
            ObjectCreate(name, OBJ_TREND, 0, StartTime, lvl,Time[0],lvl);
 
            ObjectSet(name, OBJPROP_STYLE, LineStyle);
            
            ObjectSet(name, OBJPROP_COLOR, Col);        
            ObjectSet(name,OBJPROP_WIDTH,LineWidth);
            ObjectSet(name,OBJPROP_RAY,false);
            ObjectSet(name,OBJPROP_BACK,AllLinesBackground); 
          
         } 
         
      
Comment(
"TimeDayOfWeek(iTime(symbol,PERIOD_D1,DateTime) ", TimeDayOfWeek(iTime(symbol,PERIOD_D1,DateTime)) , "\n", 
"EnumToString(DAYOFWEEK) ", EnumToString(DAYOFWEEK) , "\n", 
"dd ", dd , "\n", 
"day ", day , "\n", 
"high ", DoubleToStr(high,digits) , "\n",
"low ",DoubleToStr(low,digits) , "\n", 

""  
) ;
     
          
*/